package com.hx.setting.dao.imp;

import com.hx.setting.dao.AbstractJDBCDaoSupport.ResultSetHandler;
import com.hx.setting.dao.BaseDao;
import com.hx.setting.dao.Group2roleDao;
import com.hx.setting.dao.imp.JdbcBaseDaoImp;
import com.hx.setting.entity.Group2roleEntity;
import com.hx.setting.model.Condition;
import com.hx.setting.model.GeneratedCriteria.Criterion;
import com.hx.setting.model.GeneratedCriteria;
import com.hx.setting.model.Group2roleModel;
import com.hx.setting.model.Page;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
@SuppressWarnings("unchecked")
public class Group2roleDaoImp<E extends Group2roleEntity, PK extends Long> extends JdbcBaseDaoImp<E, PK> implements Group2roleDao<E, PK>
{

	private static final String		TABLE_NAME			= "hx_group2role";

	private static final String		PK_FIELD_NAME		= "id";

	private static final String		BASE_QUERY_FIELD	= "hx_group2role.id AS id ,hx_group2role.role_id AS roleId ,hx_group2role.group_id AS groupId ";

	private static final String[]	BASE_FIELD			= new String[]
														{ "id", "roleId", "groupId" };

	private static final String		BLOB_QUERY_FIELD	= null;

	private static final String[]	BLOB_FIELD			= null;

	@Override
	protected String getTableName()
	{
		return TABLE_NAME;
	}

	@Override
	protected String getFieldPrimaryKeyName()
	{
		return PK_FIELD_NAME;
	}

	@Override
	protected ResultSetHandler getResultSetHandler()
	{
		return new Group2roleDaoImp.QueryResultSetHandler();
	}

	@Override
	protected String[] getBaseField()
	{
		return BASE_FIELD;
	}

	@Override
	protected String[] getBlobField()
	{
		return BLOB_FIELD;
	}

	@Override
	protected String getBaseQueryField()
	{
		return BASE_QUERY_FIELD;
	}

	@Override
	protected String getBlobQueryField()
	{
		return BLOB_QUERY_FIELD;
	}

	@Override
	protected E getEntity(boolean hasBlob)
	{
		Group2roleEntity answer = null;
		answer = new Group2roleEntity();
		return (E) answer;
	}

	@Override
	public boolean isSupportAssociationQuery()
	{
		return true;
	}

	@Override
	public List<E> selectAllForAssociation()
	{
		return selectForAssociation( false ,null ,null ,null );
	}

	@Override
	public List<E> selectAllForAssociation(Page page)
	{
		return selectForAssociation( false ,null ,page ,null );
	}

	@Override
	public E selectByPrimaryKeyForAssociation(PK pk)
	{
		E answer = null;
		Page page = new Page( -1, -1, -1 );
		List<E> list = selectForAssociation( true ,getTableName() + ".id" + "=? " ,page ,new Object[]
		{ pk } );
		if ( list != null && list.size() > 0 )
		{
			answer = list.get( 0 );
		}
		return answer;
	}

	@Override
	public List<E> selectByFieldForAssociation(Enum field, Object value)
	{
		return selectByFieldForAssociation( field ,value ,null );
	}

	@Override
	public List<E> selectByFieldForAssociation(Enum field, Object value, Page page)
	{
		List<E> answer = null;
		answer = selectForAssociation( false ,getTableName() + "." + field + "=? " ,page ,new Object[]
		{ value } );
		return answer;
	}

	@Override
	public List<E> selectByFieldsForAssociation(Enum[] fields, Object[] values)
	{
		return selectByFieldsForAssociation( fields ,values );
	}

	@Override
	public List<E> selectByFieldsForAssociation(Enum[] fields, Object[] values, Page page)
	{
		List<E> answer = null;
		if ( fields.length != values.length )
		{
			throw new RuntimeException( "查询字段数量和值不一致" );
		}
		StringBuilder condtion = new StringBuilder();
		boolean bool = false;
		for ( int i = 0 ; i < fields.length ; i++ )
		{
			if ( bool ) condtion.append( "AND " );
			else bool = true;
			condtion.append( getTableName() ).append( '.' ).append( fields[i] ).append( "=? " );
		}
		answer = selectForAssociation( false ,condtion.toString() ,null ,values );
		return answer;
	}

	@Override
	public List<E> selectByDynamicForAssociation(Condition<? extends GeneratedCriteria> search)
	{
		return selectByDynamicForAssociation( search ,null );
	}

	@Override
	public List<E> selectByDynamicForAssociation(Condition<? extends GeneratedCriteria> search, Page page)
	{
		String condtion = null;
		Object[] params = null;
		if ( search != null && search.getOredCriteria().size() > 0 )
		{
			Object[] build = make( search );
			condtion = (String) build[0];
			params = (Object[]) build[1];
		}
		return selectForAssociation( false ,condtion ,page ,params );
	}

	private List<E> selectForAssociation(boolean hasBlob, String condtion, Page page, Object[] params)
	{
		List<E> answer = null;
		String enable = appendAvailableCondtion();
		if ( enable != null && enable.length() > 0 )
		{
			enable = "AND " + enable;
		}
		else
		{
			enable = "";
		}
		StringBuilder sql = new StringBuilder();
		StringBuilder inner = new StringBuilder();
		StringBuilder sb = new StringBuilder( appendAvailableCondtion() );
		sql.append( "SELECT " );
		sql.append( "hx_group2role.id AS id, hx_group2role.role_id AS roleId, hx_group2role.group_id AS groupId, " );
		if ( hasBlob )
		{
			sql.append( "" );
		}
		sql
				.append( "jwkm.id AS role_id, jwkm.rolename AS role_rolename, jwkm.parentrole AS role_parentrole, jwkm.depth AS role_depth, jwkm.allowassignedrecordsto AS role_allowassignedrecordsto, " );
		sql.append( "aVZz.id AS groups_id, aVZz.group_name AS groups_groupName " );
		sql.append( "FROM hx_group2role " );
		inner.append( "LEFT JOIN hx_role AS jwkm ON hx_group2role.role_id=jwkm.id " ).append( enable ).append( ' ' );
		inner.append( "LEFT JOIN hx_groups AS aVZz ON hx_group2role.group_id=aVZz.id " ).append( enable ).append( ' ' );
		sql.append( inner );
		if ( params == null || params.length == 0 )
		{
			condtion = null;
			params = null;
		}
		if ( condtion != null )
		{
			if ( sb.length() > 0 ) sb.append( "AND " );
			sb.append( condtion );
		}
		if ( sb.length() > 0 )
		{
			sql.append( "WHERE " ).append( sb );
		}
		if ( page == null )
		{
			page = new Page();
		}
		String orderby = page.getOrderBy();
		if ( orderby != null )
		{
			sql.append( "ORDER BY " ).append( getTableName() ).append( '.' ).append( orderby ).append( ' ' );
		}
		int total = page.getTotal();
		final int interval = page.getInterval();
		if ( total >= 0 )
		{
			sql.append( "LIMIT " ).append( page.getBegin() ).append( ',' ).append( page.getInterval() );
		}
		try
		{
			java.sql.Connection conn = prepareConnection();
			answer = (List<E>) query( conn ,total != 0 ,hasBlob ,sql.toString() ,new ResultSetHandler()
			{

				@Override
				public List<E> handle(ResultSet resultSet, boolean hasBlob) throws SQLException
				{
					ResultSetHandler h = new QueryAssociationResultSetHandler();
					List<E> answer = new ArrayList<E>( interval <= 0 ? 16 : interval );
					E result = null;
					while ( (result = (E) h.handle( resultSet ,hasBlob )) != null )
					{
						answer.add( result );
					}
					return answer;
				}
			} ,params );
			if ( total == 0 )
			{
				StringBuilder countsql = new StringBuilder( "SELECT count(*) FROM " ).append( getTableName() ).append( ' ' ).append( inner );
				if ( sb.length() > 0 )
				{
					countsql.append( "WHERE " ).append( sb );
				}
				int count = (Integer) query( conn ,true ,false ,countsql.toString() ,new ResultSetHandler()
				{

					@Override
					public Object handle(ResultSet resultSet, boolean hasBlob) throws SQLException
					{
						Integer answer = 0;
						if ( resultSet.next() )
						{
							answer = resultSet.getInt( 1 );
						}
						return answer;
					}
				} ,params );
				page.setTotal( count );
			}
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}
		return answer;
	}

	@Override
	public int insert(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder( "INSERT INTO " ).append( TABLE_NAME );
			sql.append( "( role_id, group_id ) VALUES ( ?, ? )" );
			Object[] params = new Object[]
			{ bean.getRoleId(), bean.getGroupId() };
			try
			{
				GeneratedKeysHandler keyHandler = new GeneratedKeysHandler();
				answer = update( prepareConnection() ,true ,sql.toString() ,keyHandler ,params );
				bean.setId( keyHandler.getGeneratedKeys() );
			}
			catch ( SQLException e )
			{
				e.printStackTrace();
			}
		}
		return answer;
	}

	@Override
	public int insertSelective(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder( "INSERT INTO " ).append( TABLE_NAME );
			StringBuilder field = new StringBuilder();
			StringBuilder value = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getRoleId();
			if ( o != null )
			{
				field.append( "role_id, " );
				value.append( "?, " );
				params.add( o );
			}
			o = bean.getGroupId();
			if ( o != null )
			{
				field.append( "group_id, " );
				value.append( "?, " );
				params.add( o );
			}
			field.deleteCharAt( field.length() - 2 );
			sql.append( "( " ).append( field ).append( ") VALUES (" );
			value.deleteCharAt( value.length() - 2 );
			sql.append( value ).append( ')' );
			try
			{
				GeneratedKeysHandler keyHandler = new GeneratedKeysHandler();
				answer = update( prepareConnection() ,true ,sql.toString() ,keyHandler ,params.toArray() );
				bean.setId( keyHandler.getGeneratedKeys() );
			}
			catch ( SQLException e )
			{
				e.printStackTrace();
			}
		}
		return answer;
	}

	@Override
	public int update(E bean)
	{
		return updateByPrimaryKey( bean );
	}

	@Override
	public int updateByPrimaryKey(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			String set = ("role_id=? ,group_id=?  ");
			answer = update( set ,"id=? " ,new Object[]
			{ bean.getRoleId(), bean.getGroupId(), bean.getId() } );
		}
		return answer;
	}

	public int updateByPrimaryKeySelective(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder();
			StringBuilder field = new StringBuilder();
			StringBuilder finalCond = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getRoleId();
			if ( o != null )
			{
				field.append( "role_id=?, " );
				params.add( o );
			}
			o = bean.getGroupId();
			if ( o != null )
			{
				field.append( "group_id=?, " );
				params.add( o );
			}
			field.deleteCharAt( field.length() - 2 );
			params.add( bean.getId() );
			answer = update( field.toString() ,"id=? " ,params.toArray() );
		}
		return answer;
	}

	@Override
	public int save(E bean)
	{
		return 0;
	}

	@Override
	public int updateByDynamic(Condition<? extends GeneratedCriteria> search, E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			String set = ("role_id=? ,group_id=? ");
			Object[] build = make( search );
			answer = update( set ,(String) build[0] ,com.hx.setting.utils.StringUtils.join( new Object[]
			{ bean.getRoleId(), bean.getGroupId() } ,(Object[]) build[1] ) );
		}
		return answer;
	}

	@Override
	public int updateByDynamicSelective(Condition<? extends GeneratedCriteria> search, E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder set = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getRoleId();
			if ( o != null )
			{
				set.append( "role_id=?, " );
				params.add( o );
			}
			o = bean.getGroupId();
			if ( o != null )
			{
				set.append( "group_id=?, " );
				params.add( o );
			}
			set.deleteCharAt( set.length() - 2 );
			Object[] build = make( search );
			answer = update( set.toString() ,(String) build[0] ,com.hx.setting.utils.StringUtils.join( params.toArray() ,(Object[]) build[1] ) );
		}
		return answer;
	}

	private class QueryResultSetHandler implements ResultSetHandler
	{

		@Override
		public Group2roleEntity handle(ResultSet resultSet, boolean hasBlob) throws SQLException
		{
			Group2roleEntity bean = null;
			if ( resultSet.next() )
			{
				bean = new Group2roleEntity();
				Object obj = null;
				obj = resultSet.getLong( "id" );
				if ( obj != null && !resultSet.wasNull() ) bean.setId( (Long) obj );
				obj = resultSet.getLong( "roleId" );
				if ( obj != null && !resultSet.wasNull() ) bean.setRoleId( (Long) obj );
				obj = resultSet.getLong( "groupId" );
				if ( obj != null && !resultSet.wasNull() ) bean.setGroupId( (Long) obj );
			}
			return bean;
		}
	}

	private class QueryAssociationResultSetHandler implements ResultSetHandler
	{

		@Override
		public Group2roleEntity handle(ResultSet resultSet, boolean hasBlob) throws SQLException
		{
			Group2roleEntity bean = null;
			if ( resultSet.next() )
			{
				bean = new Group2roleEntity();
				Object obj = null;
				obj = resultSet.getLong( "id" );
				if ( obj != null && !resultSet.wasNull() ) bean.setId( (Long) obj );
				obj = resultSet.getLong( "roleId" );
				if ( obj != null && !resultSet.wasNull() ) bean.setRoleId( (Long) obj );
				obj = resultSet.getLong( "groupId" );
				if ( obj != null && !resultSet.wasNull() ) bean.setGroupId( (Long) obj );
				//==========
				com.hx.setting.entity.RoleEntity role = new com.hx.setting.entity.RoleEntity();
				obj = resultSet.getLong( "role_id" );
				if ( obj != null && !resultSet.wasNull() ) role.setId( (Long) obj );
				obj = resultSet.getString( "role_rolename" );
				if ( obj != null && !resultSet.wasNull() ) role.setRolename( (String) obj );
				obj = resultSet.getString( "role_parentrole" );
				if ( obj != null && !resultSet.wasNull() ) role.setParentrole( com.hx.setting.model.RoleModel.ParentroleEnum.getInstance( (String) obj ) );
				obj = resultSet.getInt( "role_depth" );
				if ( obj != null && !resultSet.wasNull() ) role.setDepth( (Integer) obj );
				obj = resultSet.getShort( "role_allowassignedrecordsto" );
				if ( obj != null && !resultSet.wasNull() ) role.setAllowassignedrecordsto( (Short) obj );
				bean.setRole( role );
				com.hx.setting.entity.GroupsEntity groups = new com.hx.setting.entity.GroupsEntity();
				obj = resultSet.getLong( "groups_id" );
				if ( obj != null && !resultSet.wasNull() ) groups.setId( (Long) obj );
				obj = resultSet.getString( "groups_groupName" );
				if ( obj != null && !resultSet.wasNull() ) groups.setGroupName( (String) obj );
				bean.setGroups( groups );
				//==========
			}
			return bean;
		}
	}
}
/* Honey v3.1 Test - version:4 - OriginalChecksum:Ba333e3d49e5561e569dfdc6101a0f7df (do not edit this line) */
